package cn.fastoo.web.backstage.web.controller;

import cn.fastoo.common.dao.system.UserBaseInfoDao;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.SavedRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private UserBaseInfoDao userBaseInfoDao;

	@RequestMapping(method = RequestMethod.GET)
	public String login1(ModelMap model, HttpServletRequest request) {
		return "/frame/login2";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String login2(@RequestParam String loginName,
						  @RequestParam String password,
						  @RequestParam String languge,
						  HttpServletRequest request) {
		UsernamePasswordToken token = new UsernamePasswordToken(loginName, password,false);
		try {
			SecurityUtils.getSubject().login(token);
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return "frame/login2";
		}
		SavedRequest savedRequest = (SavedRequest)SecurityUtils.getSubject().getSession().getAttribute("shiroSavedRequest");
		String url = "/";
		//if(savedRequest!=null){
			//url=savedRequest.getRequestUrl();
		//}
		request.getSession().setAttribute("languge",languge);
		userBaseInfoDao.updateLanguge(languge,loginName);
		return "redirect:"+url;
	}




}
